<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\validate\AdminValidate;
use app\BaseController;
use app\Request;
use think\captcha\facade\Captcha;
use app\admin\model\Admin as AdminModel;

class Common extends BaseController
{
    public function login(Request $request){
        if($request->isPost()){
            $params = $request->param();

            $validate = new AdminValidate();
            if(!$validate->scene('login')->check($params)) return $this->error($validate->getError());

            $where = [
                ["username|qq|nickname","=",$params['username']],
                ['password',"=",md5($params['password'])]
            ];
            $query = AdminModel::where($where)->find();
            if(!$query) return $this->error('用户名或密码错误！');

            $expense = [
                'id'        => $query['id'],
                'name'      => empty($query['nickname']) ? $query['username'] : $query['nickname'],
                'role'      => $query['authorization'] == "超级管理员" ? 'admin' : 'visitor',
                'avatar'      =>    "http://q.qlogo.cn/g?b=qq&nk=".$query['qq']."&s=640",
            ];
            return $this->success('登录成功',[
                'token'     => createToken($query['id'],$expense)
            ]);
        }
    }

}